package com.audition.thread.volatiledemo;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * @author xukang
 * @date 2021-03-08 10:17
 */
public class Producer implements Runnable {
    BlockingQueue storage;
    public volatile boolean cancle = false;

    public Producer(BlockingQueue storage) {
        this.storage = storage;
    }
    @Override
    public void run() {
        int num = 0;
        while (num <= 10000 && !cancle) {
            try {
                if (num%50==0) {

                    storage.put(num);
                    System.out.println(num + "是50的倍数,被放到仓库中了。");
                }
                num++;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
//                System.out.println("生产者停止了线程");
            }

        }
    }
    static class Consumer {
        BlockingQueue storage;

        public Consumer(BlockingQueue storage) {
            this.storage = storage;
        }

        public boolean needMoreNums() {
            if (Math.random()>0.97) {
                return false;
            }
            return true;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ArrayBlockingQueue storage = new ArrayBlockingQueue<>(8);
        Producer producer = new Producer(storage);
        Thread producerThread = new Thread(producer);
        producerThread.start();
        Thread.sleep(500);
        Consumer consumer = new Consumer(storage);
        while (consumer.needMoreNums()) {
            System.out.println(consumer.storage.take() + "被消费了");
            Thread.sleep(100);
            System.out.println("消费者不需要更多数据了。");

            //一旦消费不需要更多数据了，我们应该让生产者也停下来，但是实际情况却停不下来
            producer.cancle = true;
            System.out.println(producer.cancle);
        }
    }
}
